// components/imgbox/imgbox.js
const bgMusic = wx.getBackgroundAudioManager()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    content: {
      type: String
    },
    imgUrl_1: {
      type: String
    },
    imgUrl: {
      type: String
    },
    music: {
      type: String
    },
    title: {
      type: String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // comm: './image/'
    playing: false,
    playSrc: "./image/play.png",
    pauseSrc: "./image/pause.png",
    i: 1
  },
  attached:function(e){
    console.log(11)
    // console.log(this.properties.music)
    this._recoverStatus()
    this._monitorSwitch()
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    onPlay(){
      let i = this.data.i
      if(!this.data.playing){
        this.setData({
          playing: true
        })
        // 解决背景音乐暂停后再播放重新播放的bug
        if (decodeURI(bgMusic.src) !== this.properties.music) {
          bgMusic.src = this.properties.music
          console.log(decodeURI(bgMusic.src))
          console.log(this.properties.music)
          bgMusic.title = this.properties.title
        } else {
          bgMusic.play()
        }
      }else {
        this.setData({
          playing: false
        })
        bgMusic.pause()
      }
      bgMusic.onEnded(() => {
        this.setData({
          playing: false
        })
      })
    },
    _recoverStatus(){
      console.log(Boolean(bgMusic.paused))
      if (bgMusic.paused) {
        this.setData({
          playing: false
        })
        return;
      }
      if (decodeURI(bgMusic.src) == this.properties.music) {
        this.setData({
          playing: true
        })
      }
    },
    _monitorSwitch:function(){
      bgMusic.onPlay(()=>{
        this._recoverStatus()
      })
      bgMusic.onPause(() => {
        this._recoverStatus()
      })
      bgMusic.onStop(() => {
        this._recoverStatus()
      })
    }
  }
})
